package com.xof.phonecall

import android.annotation.SuppressLint
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.annotation.RequiresApi
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.xof.phonecall.base.BaseActivity
import com.xof.phonecall.databinding.ActivitySpalshBinding
import com.xof.phonecall.extipetrpide.startToNextActivity
import com.xof.phonecall.extipetrpide.startToNextActivityAndFinish
import com.xof.phonecall.helpipetrpide.UserStateHelp
import com.xof.phonecall.helpipetrpide.versionUtil.ZHIVersionIMPL
import com.xof.phonecall.telecom.MainActivity
import com.xof.phonecall.valipetrpide.ValGlobalConstant
import com.xof.phonecall.valipetrpide.ValPoint

class SpalshActivity : BaseActivity() {

    lateinit var mActivityBinding: ActivitySpalshBinding

    private val mViewModelLaunch by lazy {
        ViewModelProvider(this).get(ViewModelLaunch::class.java)
    }

    private val mViewModelLogin by lazy {
        ViewModelProvider(this).get(ViewModelLogin::class.java)
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mViewModelLaunch)
        list.add(mViewModelLogin)
        return list
    }

    override fun onBindingViewTag(): String {
        return ValPoint.LOGIN.name
    }

    override fun onBindingContentView(): View {
        mActivityBinding = ActivitySpalshBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetTextI18n")
    override fun onContentViewBinded() {
        try {
            //测试
//            UserStateHelp.logout()

            //版本检查返回
            mViewModelLaunch.LiveDataCetAppVersion.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        //检测登录状态
                        val needUpdate = ZHIVersionIMPL.handlerAppVersion(this, result)
                        if (!needUpdate) {
                            checkLoginState()
                        }
                    }
                }
            }

            //登录返回
            mViewModelLogin.LiveDataDoLogin.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        //转到电话页
                        val hashMap = HashMap<String, String>()
                        startToNextActivity<MainActivity>(params = hashMap)
                    }
                }
            }

            //去登录
            mActivityBinding.textVersion.text = "Version: ${ValGlobalConstant.VAL_APP_VERSION_BUILD}(${ValGlobalConstant.VAL_APP_VERSION})"

            getNeedPermissions()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()
    }

    @RequiresApi(Build.VERSION_CODES.Q)
    @SuppressLint("SetTextI18n")
    override fun onPermissionsRequestAllPassipetrpide() {
//        if (MyApplication.IS_DEBUG) {
//            //测试
//            Handler(Looper.getMainLooper()).postDelayed({
//                checkLoginState()
//            }, 2000)
//        } else {
//            mViewModelLaunch.checkAppVersion()
//        }
        //测试
//        Handler(Looper.getMainLooper()).postDelayed({
//            checkLoginState()
//        }, 2000)

        requstDefaultDialer()
    }

    override fun onDialerRoleRequestAllPass() {
        Handler(Looper.getMainLooper()).postDelayed({
            checkLoginState()
        }, 2000)
    }

    fun checkLoginState() {
        if (UserStateHelp.isDidLogin()) {
            val hashMap = HashMap<String, String>()
            startToNextActivityAndFinish<OutgoingCallActivity>(params = hashMap)
        } else {
            val hashMap = HashMap<String, String>()
            startToNextActivityAndFinish<LoginActivity>(params = hashMap)
        }
    }
}